package server.events;

import database.BeePool;
import server.Randomizer;
import tools.FilePrinter;
import tools.FileoutputUtil;
import tools.Pair;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class MapleOxQuizFactory {
    private boolean initialized;
    private Map<Pair<Integer, Integer>, MapleOxQuizEntry> questionCache;
    private static MapleOxQuizFactory instance;

    public MapleOxQuizFactory() {
        this.initialized = false;
        this.questionCache = (Map<Pair<Integer, Integer>, MapleOxQuizEntry>) new HashMap();
    }

    public static MapleOxQuizFactory getInstance() {
        return MapleOxQuizFactory.instance;
    }

    public boolean hasInitialized() {
        return this.initialized;
    }

    public Entry<Pair<Integer, Integer>, MapleOxQuizEntry> grabRandomQuestion() {
        int size = this.questionCache.size();
        Entry<Pair<Integer, Integer>, MapleOxQuizEntry> oxquiz = null;
        Block_2:
        while (true) {
            Iterator<Entry<Pair<Integer, Integer>, MapleOxQuizEntry>> iterator = this.questionCache.entrySet().iterator();
            while (iterator.hasNext()) {
                oxquiz = (Entry<Pair<Integer, Integer>, MapleOxQuizEntry>) iterator.next();
                if (Randomizer.nextInt(size) == 0) {
                    break Block_2;
                }
            }
        }
        return oxquiz;
    }

    public void initialize() {
        if (this.initialized) {
            return;
        }
        System.out.println("[" + FileoutputUtil.CurrentReadable_Time() + "][信息]:初始化0X答题题库");
        try (Connection con = BeePool.getConnection();
             PreparedStatement ps = con.prepareStatement("SELECT * FROM wz_oxdata");
             ResultSet rs = ps.executeQuery()) {
            while (rs.next()) {
                this.questionCache.put(new Pair(Integer.valueOf(rs.getInt("questionset")), Integer.valueOf(rs.getInt("questionid"))), this.get(rs));
            }
            rs.close();
            ps.close();
            
        } catch (Exception e) {
            FilePrinter.printError("MapleOxQuizEntry.txt", (Throwable) e);
            FileoutputUtil.outError("logs/资料库异常.txt", (Throwable) e);
        }
        this.initialized = true;
    }

    public MapleOxQuizEntry getFromSQL(String sql) {
        MapleOxQuizEntry ret = null;
        try (Connection con = BeePool.getConnection();
             PreparedStatement ps = con.prepareStatement(sql);
             ResultSet rs = ps.executeQuery()) {
            if (rs.next()) {
                ret = this.get(rs);
            }
            rs.close();
            ps.close();
            
        } catch (Exception e) {
            FilePrinter.printError("MapleOxQuizEntry.txt", (Throwable) e);
            FileoutputUtil.outError("logs/资料库异常.txt", (Throwable) e);
        }
        return ret;
    }

    public static MapleOxQuizEntry getOxEntry(int questionSet, int questionId) {
        return getInstance().getOxQuizEntry(new Pair(Integer.valueOf(questionSet), Integer.valueOf(questionId)));
    }

    public static MapleOxQuizEntry getOxEntry(Pair<Integer, Integer> pair) {
        return getInstance().getOxQuizEntry(pair);
    }

    public MapleOxQuizEntry getOxQuizEntry(Pair<Integer, Integer> pair) {
        MapleOxQuizEntry mooe = (MapleOxQuizEntry) this.questionCache.get(pair);
        if (mooe == null) {
            if (this.initialized) {
                return null;
            }
            mooe = this.getFromSQL("SELECT * FROM wz_oxdata WHERE questionset = " + pair.getLeft() + " AND questionid = " + pair.getRight());
            this.questionCache.put(pair, mooe);
        }
        return mooe;
    }

    private MapleOxQuizEntry get(ResultSet rs) throws SQLException {
        return new MapleOxQuizEntry(rs.getString("question"), rs.getString("display"), this.getAnswerByText(rs.getString("answer")), rs.getInt("questionset"), rs.getInt("questionid"));
    }

    private int getAnswerByText(String text) {
        if (text.equalsIgnoreCase("x")) {
            return 0;
        }
        if (text.equalsIgnoreCase("o")) {
            return 1;
        }
        return -1;
    }

    public void reloadOX() {
        this.questionCache.clear();
        try (Connection con = BeePool.getConnection();
             PreparedStatement ps = con.prepareStatement("SELECT * FROM wz_oxdata");
             ResultSet rs = ps.executeQuery()) {
            while (rs.next()) {
                this.questionCache.put(new Pair(Integer.valueOf(rs.getInt("questionset")), Integer.valueOf(rs.getInt("questionid"))), this.get(rs));
            }
            rs.close();
            ps.close();
        } catch (Exception e) {
            FilePrinter.printError("MapleOxQuizEntry.txt", (Throwable) e);
            FileoutputUtil.outError("logs/资料库异常.txt", (Throwable) e);
        }
    }

    static {
        instance = new MapleOxQuizFactory();
    }

    public static class MapleOxQuizEntry {
        private String question;
        private String answerText;
        private int answer;
        private int questionset;
        private int questionid;

        public MapleOxQuizEntry(String question, String answerText, int answer, int questionset, int questionid) {
            this.question = question;
            this.answerText = answerText;
            this.answer = answer;
            this.questionset = questionset;
            this.questionid = questionid;
        }

        public String getQuestion() {
            return this.question;
        }

        public String getAnswerText() {
            return this.answerText;
        }

        public int getAnswer() {
            return this.answer;
        }

        public int getQuestionSet() {
            return this.questionset;
        }

        public int getQuestionId() {
            return this.questionid;
        }
    }
}
